﻿package 
{
	import flash.display.Graphics;
	import flash.display.Shape;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import fl.events.SliderEvent;
	import idv.etrex.display.*;
	import idv.etrex.display.object.DynamicStroke;
	import idv.etrex.events.PlayerEvent;
	/**
	 * ...
	 * @author etrex
	 */
	public class DynamicStrokeTool extends Sprite 
	{
		public var ds:DynamicStroke = new DynamicStroke();
		public var svg:SVG = new SVG(621,355);
		public var selectedColor:uint = 0;
		public var selectedStrokeSize:int = 3;

		public function DynamicStrokeTool():void 
		{			
			ds.x = drawArea.x;
			ds.y = drawArea.y;
			this.addChild(ds);
			ds.graphicsBat.add(svg);
			ds.graphicsBat.lineStyle(selectedStrokeSize, selectedColor);
			ds.playingRate = 3;
			
			
			// add mouse paint event
			drawArea.addEventListener(MouseEvent.MOUSE_DOWN, whenMouseDown);
			drawArea.addEventListener(MouseEvent.MOUSE_UP, whenMouseUp);
			stage.addEventListener(MouseEvent.MOUSE_UP, whenMouseUp);
			
			// add replayButton click event
			replayButton.addEventListener(MouseEvent.CLICK, replayButtonClick);
			clearButton.addEventListener(MouseEvent.CLICK, clearButtonClick);
			
			// add slider change event
			slider.addEventListener(SliderEvent.CHANGE, sliderChanged);
			
			// add slider2 change event
			slider2.addEventListener(SliderEvent.CHANGE, sliderChanged2);
			
			// add colorPicker change event
			colorPicker.addEventListener(Event.CHANGE,colorChange);
		}
		
		// mouse paint event
		public function whenMouseDown(e:MouseEvent):void {
			//moveTo
			ds.graphicsBat.moveTo(drawArea.mouseX, drawArea.mouseY);
			drawArea.addEventListener(MouseEvent.MOUSE_MOVE, whenMouseMove);
		}
		public function whenMouseUp(e:MouseEvent):void {
			//text output
			textOutput();
			drawArea.removeEventListener(MouseEvent.MOUSE_MOVE, whenMouseMove);
		}
		public function whenMouseMove(e:MouseEvent):void {
			//lineTo
			ds.graphicsBat.lineTo(drawArea.mouseX, drawArea.mouseY);
		}
		
		// button click event
		public function replayButtonClick(e:MouseEvent):void {
			ds.replay();
			
			//disable mouse operation
			replayButton.removeEventListener(MouseEvent.CLICK, replayButtonClick);
			clearButton.removeEventListener(MouseEvent.CLICK, clearButtonClick);
			ds.addEventListener(PlayerEvent.COMPLETE, dsCompleteHandler);
		}
		public function clearButtonClick(e:MouseEvent):void {
			//clear the canvas
			ds.stop();
			ds.xgraphics.clearXML();
			svg.clearXML();
			ds.graphicsBat.lineStyle(selectedStrokeSize, selectedColor);
			textOutput();
		}
		
		public function textOutput():void {
			XGraphicsText.text = ds.xgraphics.toString();
			SVGText.text = svg.toString();
		}
		
		public function dsCompleteHandler(e:PlayerEvent):void{			
			replayButton.addEventListener(MouseEvent.CLICK, replayButtonClick);
			clearButton.addEventListener(MouseEvent.CLICK, clearButtonClick);
		}
		
		//replay speed
		public function sliderChanged(e:SliderEvent):void {
			var replaySpeed = (e.target.value + 1);
			ds.playingRate = replaySpeed;
            sliderLabel.text = "replay speed: " + replaySpeed;
        }
		
		//select stroke size
		public function sliderChanged2(e:SliderEvent):void {
            selectedStrokeSize = e.target.value+1;
            sliderLabel2.text = "strokeSize: " + selectedStrokeSize;
			ds.graphicsBat.lineStyle(selectedStrokeSize,selectedColor);
        }
		
		public function colorChange(e:Event):void {
			selectedColor = ColorPicker(e.target).selectedColor;
			ds.graphicsBat.lineStyle(selectedStrokeSize,selectedColor);
		}
	}
}